<?php
namespace app\api\controller;

use app\api\ApiBaseController;
use app\common\model\MessageModel;
use app\common\model\MessageUserModel;
use app\common\model\UserModel;

class Message extends ApiBaseController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @throws \Exception
     */
    public function add()
    {
        $content = $this->input('content');
        $this->checkParams($content);

        $row = MessageModel::create([
            'user_id'=>$this->user_id,
            'content'=>$content,
            'create_time'=>time(),
        ]);

        $this->output($this->_detail($row['id'], $this->user_id));
    }

    /**
     * 详情页面
     * @param $id
     * @throws \Exception
     */
    public function detail($id)
    {
        $this->checkParams($id);

        $data = $this->_detail($id, $this->user_id);

        $this->output($data);
    }

    /**
     * 详情内容
     * @param $id
     * @throws \Exception
     */
    public function content($id)
    {
        $this->checkParams($id);

        //判断权限
        $canUserRead = $this->canUserRead($id, $this->user_id);
        if (!$canUserRead['can_read']) {
            exception($canUserRead['reason'], 403);
        }

        //设置已阅状态
        if (!$canUserRead['is_mine']) {
            MessageUserModel::create([
                'message_id' => $id,
                'user_id' => $this->user_id,
                'create_time' => time(),
            ]);
        }

        $data = [
            'content' => [
                ['type'=>'text','data'=>$canUserRead['message']['content']],
            ],
        ];
        $this->output($data);
    }

    /**
     * 详情页面的数据
     * @param $message_id
     * @param $user_id
     * @return array
     * @throws \Exception
     */
    private function _detail($message_id, $user_id)
    {
        $canUserRead = $this->canUserRead($message_id, $user_id);
        $messageRow = $canUserRead['message'];
        $is_mine = $canUserRead['is_mine'];

        $data = [
            'is_mine' => $is_mine,//是否我发布的
            'can_read' => $canUserRead['can_read'],//能否查看
            'reason' => $canUserRead['reason'],
            'time' => date('Y-m-d', $messageRow['create_time']),
        ];

        $userRow = UserModel::get($messageRow['user_id']);
        if (empty($userRow)) {
            exception('该记录不存在', 500);
        }
        $data['user'] = [
            'nickname' => $userRow['nickname'],
            'avatar' => $userRow['avatar'],
        ];
        return $data;
    }

    /**
     * 判断用户能否看消息
     * @param $message_id
     * @param $user_id
     * @return array
     * @throws \Exception
     */
    private function canUserRead($message_id, $user_id)
    {
        $messageRow = MessageModel::get($message_id);
        if (empty($messageRow)) {
            exception('该消息不存在', 404);
        }

        $is_mine = $messageRow['user_id'] == $user_id;

        if ($is_mine) {
            $can_read = true;
            $reason = '';
        } else {
            $readCount = MessageUserModel::readCount($user_id, $message_id);
            if ($readCount) {
                $can_read = false;
                $reason = '此消息已销毁';
            } else {
                $can_read = true;
                $reason = '';
            }
        }

        return [
            'message' => $messageRow,
            'is_mine' => $is_mine,
            'can_read' => $can_read,
            'reason' => $reason,
        ];
    }
}
